package com.bing.habit.controller.user;

import com.bing.habit.entity.Habit;
import com.bing.habit.model.result.Result;
import com.bing.habit.model.result.ResultFactory;
import com.bing.habit.service.ClockInRecordService;
import com.bing.habit.service.HabitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户对习惯的操作
 */
@Slf4j
@RestController
@RequestMapping("/habit")
public class HabitController {

    @Autowired
    HabitService habitService;
    @Autowired
    ClockInRecordService clockinRecordService;

    /**
     * 新增/更新 习惯
     */
    @PostMapping
    public Result addOrUpdate(@RequestBody Habit habit) {
        if (habitService.addOrUpdate(habit) && habit.getId() == 0) {
            return ResultFactory.buildSuccessResult("添加成功");
        } else if (habitService.addOrUpdate(habit) && habit.getId() != 0) {
            return ResultFactory.buildSuccessResult("编辑成功");
        } else {
            return ResultFactory.buildFailResult("习惯已存在");
        }

    }

    /**
     * 删除习惯
     *
     * @param habitId
     * @return
     */
    @DeleteMapping("/{habitId}")
    public Result delete(@PathVariable("habitId") int habitId) {
        habitService.delete(habitId);
        return ResultFactory.buildSuccessResult("删除成功");
    }

    /**
     * 根据id获取习惯
     *
     * @param habitId
     * @return
     */
    @GetMapping("/{habitId}")
    public Result get(@PathVariable("habitId") int habitId) {

        Habit habit = habitService.get(habitId);
        return ResultFactory.buildSuccessResult(habit);
    }

    /**
     * 列出全部习惯
     *
     * @return // @RequestParam("page") int page,@RequestParam("size") int size
     */
    @GetMapping("/list")
    public Result list() {
        return null;
    }

    /**
     * 列出我的习惯
     *
     * @return
     */
    @GetMapping("/list_mine/{userId}")
    public Result listByUser(@PathVariable("userId") int uid) {
        List<Habit> habits = habitService.listByUid(uid);
        for (Habit habit : habits) {
            int hid = habit.getId();
            // 昨日与今日都没有  当前连续设置为0
            if (!clockinRecordService.listByYesterday(hid) && !clockinRecordService.listByToday((hid))) {
                habit.setCurrentContinue(0);
            // 昨天没有今天有 当前连续设置为1
            } else if (!clockinRecordService.listByYesterday(hid) && clockinRecordService.listByToday((hid))) {
                habit.setCurrentContinue(1);
            }
            // 查询今天是否打卡
            if (clockinRecordService.listByToday(habit.getId())) {
                habit.setClockToday(true);
            } else {
                habit.setClockToday(false);
            }
            habitService.addOrUpdate(habit);
        }
        return ResultFactory.buildSuccessResult(habits);
    }

    /**
     * 列出习惯库：默认习惯？
     * uid == 1?
     *
     * @return
     */
    @GetMapping("/list_default")
    public Result listDefault() {
        List<Habit> habits = habitService.listByUid(1);
        return ResultFactory.buildSuccessResult(habits);
    }


}
